package com.example.chcrm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chcrm.entity.common.RandomHelper;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.employee.Department;
import com.example.chcrm.entity.employee.EssEmployee;
import com.example.chcrm.entity.employee.RoleModels;
import com.example.chcrm.mapper.EssEmployeeMapper;
import com.example.chcrm.mapper.RoleModelsMapper;
import com.example.chcrm.service.EssEmployeeService;
import com.example.chcrm.service.RoleModelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 邹
 * @Date 2025/4/18 21:45
 * @Version 1.0
 */
@Service
public class RoleModelsServiceImpl<T> implements RoleModelsService<T> {
    @Autowired
    private RoleModelsMapper roleModelsMapper;

    @Override
    @Transactional
    public ResponseMsg<Object> selectAllRole() {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        QueryWrapper<RoleModels> query = new QueryWrapper<>();
        query.select("id,role_nm");
        query.eq("is_deleted", 0);
        List<Map<String, Object>> essRole = roleModelsMapper.selectMaps(query);
        msg.setData(essRole);
        msg.success("查询成功");
        return msg;
    }

    @Override
    public ResponseMsg<IPage<RoleModels>> pageList(RoleModels roleModels) {
        // 动态条件构建
        QueryWrapper<RoleModels> queryWrapper = new QueryWrapper<>();

        // 角色名称模糊查询
        if (StringUtils.isNotBlank(roleModels.getRoleNm())) {
            queryWrapper.like("role_nm", roleModels.getRoleNm());
        }
        // 状态筛选
        if (roleModels.getStatus() != null) {
            queryWrapper.eq("status", roleModels.getStatus());
        }
        // 基础条件
        queryWrapper
                .eq("is_deleted", 0)  // 未删除数据
                .orderByDesc("create_time");

        // 检查传入的department.getPage()和getRows()值
        // 执行分页查询
        List<RoleModels> list = roleModelsMapper.selectList(queryWrapper);
        ResponseMsg<IPage<RoleModels>> iPageResponseMsg = new ResponseMsg<>();
        Page<RoleModels> page = new Page<>();
        page.setRecords(list);
        page.setTotal(list.size());
        page.setSize(roleModels.getRows());
        page.setCurrent(roleModels.getPage());
        iPageResponseMsg.setData(page);
        iPageResponseMsg.success("查询成功");
        return iPageResponseMsg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> add(RoleModels roleModels) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (StringUtils.isBlank(roleModels.getRoleNm())){
            msg.fail("请填写角色名称");
            return msg;
        }
        //验证部门名称是否存在
        int num = (roleModelsMapper.selectCount(new QueryWrapper<RoleModels>()
                .eq("role_nm",roleModels.getRoleNm())
                .eq("is_deleted",0))).intValue();
        if(num > 0){
            msg.fail("角色已存在");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        String id = RandomHelper.uuid();
        roleModels.setId(id);
        roleModels.setStatus(1);
        roleModels.setIsDeleted(0);
        roleModels.setCreateTime(new Date());
        roleModelsMapper.insert(roleModels);
        //添加操作日志
//        OperateRecord ope = new OperateRecord();
//        ope.setId(RandomHelper.uuid());
//        ope.setOperateType(OperateRecord.OPE_ADD);
//        ope.setOperationId(RandomHelper.uuid());
//        ope.setSourceId(id);
//        ope.setOperationName("admin");
//        operateRecordMapper.insert(ope);

        msg.success("新增成功");
        return msg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> disable(RoleModels dept) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (StringUtils.isBlank(dept.getId())){
            msg.fail("未找到选中角色");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        UpdateWrapper updateWrapper=  new UpdateWrapper<RoleModels>();

        if (dept.getStatus()==1){
            updateWrapper.set("status",1);
            updateWrapper.eq("id",dept.getId());
            roleModelsMapper.update(null,updateWrapper);
            msg.success("启动成功");
        }else if(dept.getStatus()==0){
            updateWrapper.set("status",0);
            updateWrapper.eq("id",dept.getId());
            roleModelsMapper.update(null,updateWrapper);
            msg.success("禁用成功");
        }else {
            msg.fail("本次操作失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return msg;
    }

    @Override
    @Transactional
    public ResponseMsg<Object> update(RoleModels dept) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (StringUtils.isBlank(dept.getId())){
            msg.fail("未找到角色");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        if (StringUtils.isBlank(dept.getRoleNm())){
            msg.fail("请填写角色名称");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        if (StringUtils.isBlank(dept.getExplaind())){
            dept.setExplaind("");
            return msg;
        }
        QueryWrapper queryWrapper = new QueryWrapper<RoleModels>().eq("is_deleted",0)
                .eq("explaind",dept.getExplaind()).eq("role_nm",dept.getRoleNm());
        int num = (roleModelsMapper.selectCount(queryWrapper)).intValue();
        if(num > 0){
            msg.fail("角色已存在");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        UpdateWrapper updateWrapper=  new UpdateWrapper<RoleModels>();
        System.out.println(dept);
        if ( StringUtils.isNotBlank(dept.getRoleNm())){
            updateWrapper.set("role_nm",dept.getRoleNm());
        }
        if ( StringUtils.isNotBlank(dept.getExplaind())){
            updateWrapper.set("explaind",dept.getExplaind());
        }
        updateWrapper.eq("id",dept.getId());
        roleModelsMapper.update(null,updateWrapper);
        msg.success("修改成功");
        return msg;
    }

    @Override
    public ResponseMsg<Object> delete(String id) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        RoleModels department = roleModelsMapper.selectById(id);
        if (department == null) {
            msg.fail("删除失败,角色未找到");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return msg;
        }
        roleModelsMapper.update(null,new UpdateWrapper<RoleModels>().set("is_deleted",-1).eq("id",department.getId()));
        msg.success("删除成功");
        return msg;
    }
}
